package com.atguigu.gmall.all.contoller;

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: 李旭
 * @create: 2021-07-20 15:13
 *
 * 购物车
 **/
@Controller
public class CartInfoController {

    @Autowired
    private CartFeignClient cartFeignClient;
    //加入购物车
    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes
      , HttpServletRequest request){//加入
        //String userId = AuthContextHolder.getUserId(request);
        //String userTempId = AuthContextHolder.getUserTempId(request);
        cartFeignClient.addCart(skuId, skuNum);
        redirectAttributes.addAttribute("sid",skuId);
        redirectAttributes.addAttribute("pc",skuNum);
        return "redirect:http://cart.gmall.com/toCart.html";//reqeust域   forward  redirect
    }
    //去显示购物车商品已经加入成功了
    @GetMapping("/toCart.html")//显示
    public String toCart(Long sid,Integer pc,Model model){
        CartInfo cartInfo = cartFeignClient.getCartInfoBySkuId(sid);
        cartInfo.setSkuNum(pc);
        model.addAttribute("cartInfo",cartInfo);
        //加入成功页面
        return "cart/addCart";
    }
    //去购物车结算
    @GetMapping("/cart.html")
    public String cartIndex(){
        return "cart/index";
    }
}
